package com.iweb.controller;

import com.iweb.pojo.Category;
import com.iweb.service.CategoryService;
import com.iweb.service.PlaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@Transactional
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    CategoryService categoryService;
    @Autowired
    PlaceService placeService;


    @RequestMapping(value = "/categoryCenter")
    public String categoryCenter(HttpServletRequest request) {
        int userID = Integer.parseInt(request.getSession().getAttribute("userID").toString());
        List list = categoryService.selectList(userID);
        /*for (int i = 0; i < list.size(); i++) {
            String info = "会员等级为"+"可享受"+"折扣";
            System.out.println(list.get(i));
        }*/
        request.setAttribute("list",list);
        return "user/category/categoryCenter";
    }

    @RequestMapping("/getCategory")
    @ResponseBody
    public void getCategory(HttpServletResponse resp) throws IOException {
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=UTF-8");
        List categories = categoryService.selectList(5);
        for (int i = 0; i < categories.size(); i++) {
            System.out.println(categories.get(i));
        }
        resp.getWriter().print(categories);

    }

    @RequestMapping(value = "/insertCategory")
    public String insertCategory(HttpServletRequest req, HttpServletResponse response, Category category) {

        if (categoryService.insert(category)) {
            req.setAttribute("places", placeService.selectPlace());
            return "user/place/book";
        } else {
            req.setAttribute("message2", "未成功");
            return "user/place/placeShow";
        }

    }

    @RequestMapping(value = "/updateCategory")
    public String updateCategory(HttpServletRequest request, HttpServletResponse response, Category category) {

        Boolean flag = categoryService.update(category);
        System.out.println(flag);
        return "user/place/book";
    }
}
